package com.example.gzysmartcity.ui.fragment.sell;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.example.gzysmartcity.databinding.FragmentExpendBinding;
import com.example.gzysmartcity.repository.Js;
import com.example.gzysmartcity.repository.model.ConsumptionWebInfo;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.ExpendViewModel;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static com.example.gzysmartcity.utils.ColorUtil.getRandColor;

public class ExpendFragment extends BaseFragment<FragmentExpendBinding> {
    @Override
    protected FragmentExpendBinding viewBinding() {
        return FragmentExpendBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }


    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ExpendViewModel expendViewModel = new ViewModelProvider(this).get(ExpendViewModel.class);
        expendViewModel.getSales();

        expendViewModel.getMutableLiveData().observe(getViewLifecycleOwner(), integer -> {
            switch (integer) {
                case 1:
                    b.hBarWebView.getSettings().setJavaScriptEnabled(true);
                    b.hBarWebView.addJavascriptInterface(new Js<>(expendViewModel.getList()), "data");
                    b.hBarWebView.loadUrl("file:///android_asset/consumption.html");
                    break;
                case 2:
                    LineData lineData = new LineData();
                    int i2 = 0;
                    List<Entry> entryList = new ArrayList<>();
                    List<ConsumptionWebInfo> list2 = expendViewModel.getList2();
                    for (ConsumptionWebInfo s : list2) {
                        entryList.add(new BarEntry(i2, s.getValue()));
                        i2++;
                    }
                    LineDataSet lineDataSet = new LineDataSet(entryList, "消费金额");
                    lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
                    lineDataSet.setDrawFilled(true);
                    lineDataSet.setDrawCircles(false);
                    lineData.addDataSet(lineDataSet);
                    XAxis xAxis2 = b.line.getXAxis();
                    xAxis2.setDrawGridLines(false);
                    xAxis2.setPosition(XAxis.XAxisPosition.BOTTOM);
                    xAxis2.setCenterAxisLabels(true);
                    xAxis2.setValueFormatter(new ValueFormatter() {
                        @Override
                        public String getFormattedValue(float value) {
                            return list2.get((int) (Math.abs(value) % 3)).getName();
                        }
                    });

                    YAxis axisLeft2 = b.line.getAxisLeft();
                    axisLeft2.setDrawAxisLine(false);
                    YAxis yAxis2 = b.line.getAxisRight();
                    yAxis2.setDrawGridLines(true);
                    yAxis2.setDrawLabels(false);
                    yAxis2.setDrawAxisLine(false);
                    Legend legend2 = b.line.getLegend();
                    legend2.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
                    legend2.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
                    legend2.setOrientation(Legend.LegendOrientation.HORIZONTAL);
                    Description description2 = b.line.getDescription();
                    description2.setText("消费统计");
//                    description2.setPosition(120, 25);
                    description2.setTextSize(14);
                    b.line.setData(lineData);
                    b.line.setScaleEnabled(false);
                    b.line.invalidate();
                    break;
                case 3:
//bar
                    BarData barData = new BarData();
                    for (ConsumptionWebInfo s : expendViewModel.getList3()) {
                        List<BarEntry> barEntries = new ArrayList<>();
                        barEntries.add(new BarEntry(0, s.getValue()));
                        BarDataSet barDataSet = new BarDataSet(barEntries, s.getName());
                        barDataSet.setColor(Color.parseColor(getRandColor()));
                        barData.addDataSet(barDataSet);
                    }
                    XAxis xAxis3 = b.bar.getXAxis();
                    xAxis3.setGranularity(1f);
                    xAxis3.setAxisMinimum(0);
                    xAxis3.setAxisMaximum(1);
                    xAxis3.setDrawGridLines(false);
                    xAxis3.setPosition(XAxis.XAxisPosition.BOTTOM);
                    xAxis3.setCenterAxisLabels(true);
                    xAxis3.setValueFormatter(new ValueFormatter() {
                        @Override
                        public String getFormattedValue(float value) {
                            return "商品名称";
                        }
                    });

                    YAxis axisLeft3 = b.bar.getAxisLeft();
                    axisLeft3.setAxisMinimum(0);
                    axisLeft3.setDrawAxisLine(false);
                    axisLeft3.setSpaceTop(20);
                    YAxis yAxis3 = b.bar.getAxisRight();
                    yAxis3.setAxisMinimum(0);
                    yAxis3.setDrawGridLines(true);
                    yAxis3.setDrawLabels(false);
                    yAxis3.setDrawAxisLine(false);
                    yAxis3.setSpaceTop(20);

                    barData.setBarWidth(0.175f);
                    Legend legend3 = b.bar.getLegend();
                    legend3.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
                    legend3.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
                    legend3.setOrientation(Legend.LegendOrientation.VERTICAL);
                    Description description3 = b.bar.getDescription();
                    description3.setText("销售量");
//                    description3.setPosition(90, 120);
                    description3.setTextSize(14);
                    b.bar.setData(barData);
                    b.bar.setScaleEnabled(false);
                    b.bar.groupBars(0f, 0.08f, 0.06f);
                    b.bar.invalidate();
                    break;
            }
        });

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }


    @Override
    protected String setTitle() {
        return "消费统计";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }
}
